from google.colab import drive
drive.mount('/content/drive')
import pathlib
from pathlib import Path
import pandas as pd
import PIL
from PIL import Image, ExifTags, ImageOps
from matplotlib import image
from matplotlib import pyplot as plt
import numpy as np
import ast
import json
from scipy import signal
import os
from collections import defaultdict
import random
root_directory = "/content/drive/My Drive/2022_Projekt_badawczy/ZPD_SZUM/"
plots = root_directory + '/plots'
collected_data = root_directory + "data/"
downloaded_data = root_directory + "BAZA_Z_NETA/"
final_data = root_directory + "finaldata/"
preprocessed_data = root_directory + "preprocessed_data/"
dataset_url = root_directory
collected_data_dir = pathlib.Path(collected_data)
downloaded_data_dir = pathlib.Path(downloaded_data)
final_data_dir = pathlib.Path(final_data)
collected_data_dir, downloaded_data_dir, final_data_dir
print("Collected data")
image_count = len(list(collected_data_dir.glob('*/*.jpg')))
print("jpg images: ", image_count)
image_count = len(list(collected_data_dir.glob('*/*.*')))
print("all images: ", image_count)
print("Downloaded data")
image_count = len(list(downloaded_data_dir.glob('*/*.jpg')))
print("jpg images: ", image_count)
image_count = len(list(downloaded_data_dir.glob('*/*.*')))
print("all images: ", image_count)
collected_images = []
downloaded_images = []
class_names = ['bicycle', 'bus', 'car', 'motorcycle', 'truck', 'TRUDNOSC_Z_PRZYPISANIEM']
print("Number of images for particular classes collected by hand")
for class_name in class_names:
dir = class_name + '/*.jpg'
image_count = len(list(collected_data_dir.glob(dir)))
collected_images.append(image_count)
print(class_name, ':', image_count)
print()
print("Number of images for particular classes downloaded online")
for class_name in class_names:
dir = class_name + '/*.*'
image_count = len(list(downloaded_data_dir.glob(dir)))
downloaded_images.append(image_count)
print(class_name, ':', image_count)
df = pd.DataFrame(class_names, columns=['class_name'])
df['collected_images'] = collected_images
df['downloaded_images'] = downloaded_images
df['sum_of_images_in_the_class'] = df.collected_images + df.downloaded_images
display(df)
print()
print("Total sum of all images (collected + downloaded): ", sum(df['sum_of_images_in_the_class']))
print("Total sum of collected images: ", sum(df['collected_images']))
print("Total sum of downloaded images : ", sum(df['downloaded_images']))
(initially without any changes to the files, we collect data about the photos)
SCRIPT EXECUTION TIME
class_names = ['motorcycle', 'bicycle', 'bus', 'car', 'truck']
images_names_list = list()
images_modes_list = list()
images_formats_list = list()
images_sizes_list = list()
images_red_pixels_list = list()
images_green_pixels_list = list()
images_blue_pixels_list = list()
images_sources_list = list()
metadata_datetime_list = list()
metadata_resolutionunit_list = list()
metadata_Xresolution_list = list()
metadata_Yresolution_list = list()
metadata_brightnessvalue_list = list()
metadata_aperturevalue_list = list()
metadata_phonemake_list = list()
metadata_phonemodel_list = list()
metadata_ISOspeedratings_list = list()
metadata_flash_list = list()
metadata_focallength_list = list()
metadata_sensingmethod_list = list()
for class_name in class_names:
#each class starts id from 0
imageid = 0
#paths
imageCollectedFilePath = Path.joinpath(collected_data_dir, class_name)
imageDownloadedFilePath = Path.joinpath(downloaded_data_dir, class_name)
imageFinalFilePath = Path.joinpath(final_data_dir, class_name)
#collect images from first source
for imagePath in list(imageCollectedFilePath.glob('*.*')):
image = Image.open(imagePath) # loads image (as pd.read_image())
# save in the same format
image_name = class_name + str(imageid)
image_mode = image.mode
image_format = image.format
image_size = image.size
image_source = 'collected'
pixels = image.histogram()
red_pixels = pixels[0:256] # indicates Red
green_pixels = pixels[256:512] # indicated Green
blue_pixels = pixels[512:768] # indicates Blue
#print(image_name, image_mode, image_format, image_size, red_pixels, green_pixels, blue_pixels)
#SAVE IMAGES DATA
images_names_list.append(image_name)
images_modes_list.append(image_mode)
images_formats_list.append(image_format)
images_sizes_list.append(image_size)
images_red_pixels_list.append(red_pixels)
images_green_pixels_list.append(green_pixels)
images_blue_pixels_list.append(blue_pixels)
images_sources_list.append(image_source)
#ROTATE IMAGE IF NECESSARY USING ORIENTATION TAG
# print('image before rotating')
# plt.imshow(image)
# plt.show()
#rotate if necessary using orientation tags
#image = ImageOps.exif_transpose(image)
img_exif = image.getexif()
if bool(img_exif) == False:
metadata_datetime = ''
metadata_resolutionunit = ''
metadata_Xresolution = ''
metadata_Yresolution = ''
metadata_brightnessvalue = ''
metadata_aperturevalue = ''
metadata_phonemake = ''
metadata_phonemodel = ''
metadata_ISOspeedratings = ''
metadata_flash = ''
metadata_focallength = ''
metadata_sensingmethod = ''
#print('Sorry, image has no exif data.')
else:
for key, val in img_exif.items():
#rotate if necessary using orientation tags
if key == 274 and val == 3:
image = image.rotate(180, expand=True)
elif key == 274 and val == 6:
image = image.rotate(270, expand=True)
elif key == 274 and val == 8:
image = image.rotate(90, expand=True)
#EXTRACT METADATA FROM TAGS
#datetime
if key == 306:
metadata_datetime = val
if 306 not in img_exif.keys():
metadata_datetime = ''
#metadata_resolutionunit
if key == 296:
metadata_resolutionunit = val
if 296 not in img_exif.keys():
metadata_resolutionunit = ''
#metadata_Xresolution
if key == 282:
metadata_Xresolution = val
if 282 not in img_exif.keys():
metadata_Xresolution = ''
#metadata_Yresolution
if key == 283:
metadata_Yresolution = val
if 283 not in img_exif.keys():
metadata_Yresolution = ''
#metadata_brightnessvalue
if key == 37379:
metadata_brightnessvalue = val
if 37379 not in img_exif.keys():
metadata_brightnessvalue = ''
#metadata_aperturevalue
if key == 37378:
metadata_aperturevalue = val
if 37378 not in img_exif.keys():
metadata_aperturevalue = ''
#metadata_phonemake
if key == 271:
metadata_phonemake = val
if 271 not in img_exif.keys():
metadata_phonemake = ''
#metadata_phonemodel
if key == 272:
metadata_phonemodel = val
if 272 not in img_exif.keys():
metadata_phonemodel = ''
#metadata_ISOspeedratings
if key == 34855:
metadata_ISOspeedratings = val
if 34855 not in img_exif.keys():
metadata_ISOspeedratings = ''
#metadata_flash
if key == 37385:
metadata_flash = val
if 37385 not in img_exif.keys():
metadata_flash = ''
#metadata_focallength
if key == 37386:
metadata_focallength = val
if 37386 not in img_exif.keys():
metadata_focallength = ''
#metadata_sensingmethod
if key == 41495:
metadata_sensingmethod = val
if 41495 not in img_exif.keys():
metadata_sensingmethod = ''
#if ExifTags.TAGS[key] == 'Orientation':
# print(key) #274 is the key to the 'Orientation" TAG
#print(f'{ExifTags.TAGS[key]}:{val}')
# print(img_exif)
# print(type(img_exif))
# print('image after rotating')
# plt.imshow(image)
# plt.show()
#SAVE IMAGES METADATA
metadata_datetime_list.append(metadata_datetime)
metadata_resolutionunit_list.append(metadata_resolutionunit)
metadata_Xresolution_list.append(metadata_Xresolution)
metadata_Yresolution_list.append(metadata_Yresolution)
metadata_brightnessvalue_list.append(metadata_brightnessvalue)
metadata_aperturevalue_list.append(metadata_aperturevalue)
metadata_phonemake_list.append(metadata_phonemake)
metadata_phonemodel_list.append(metadata_phonemodel)
metadata_ISOspeedratings_list.append(metadata_ISOspeedratings)
metadata_flash_list.append(metadata_flash)
metadata_focallength_list.append(metadata_focallength)
metadata_sensingmethod_list.append(metadata_sensingmethod)
#SAVE IMAGE
image.save(Path.joinpath(imageFinalFilePath, f'{image_name}.{str(image_format)}'), format=image_format)
#remember to increment image_id
imageid += 1
#optionally - show image
if imageid % 250 == 0:
plt.imshow(image)
plt.show()
#CLOSE IMAGE (necessary to release memory, because load() function were not called)
image.close()
### TO DELETE!! ##
# if imageid > 2:
# break
#collect images from second source (internet database)
for imagePath in list(imageDownloadedFilePath.glob('*.*')):
image = Image.open(imagePath) # ładuje zdjęcie (jak pd.read_image())
# save in the same format
image_name = class_name + str(imageid)
image_mode = image.mode
image_format = image.format
image_size = image.size
image_source = 'downloaded'
pixels = image.histogram()
red_pixels = pixels[0:256] # indicates Red
green_pixels = pixels[256:512] # indicated Green
blue_pixels = pixels[512:768] # indicates Blue
#print(image_name, image_mode, image_format, image_size, red_pixels, green_pixels, blue_pixels)
#SAVE IMAGES DATA
images_names_list.append(image_name)
images_modes_list.append(image_mode)
images_formats_list.append(image_format)
images_sizes_list.append(image_size)
images_red_pixels_list.append(red_pixels)
images_green_pixels_list.append(green_pixels)
images_blue_pixels_list.append(blue_pixels)
images_sources_list.append(image_source)
#ROTATE IMAGE IF NECESSARY USING ORIENTATION TAG
# print('image before rotating')
# plt.imshow(image)
# plt.show()
#rotate if necessary using orientation tags
#image = ImageOps.exif_transpose(image)
img_exif = image.getexif()
if bool(img_exif) == False:
metadata_datetime = ''
metadata_resolutionunit = ''
metadata_Xresolution = ''
metadata_Yresolution = ''
metadata_brightnessvalue = ''
metadata_aperturevalue = ''
metadata_phonemake = ''
metadata_phonemodel = ''
metadata_ISOspeedratings = ''
metadata_flash = ''
metadata_focallength = ''
metadata_sensingmethod = ''
#print('Sorry, image has no exif data.')
else:
for key, val in img_exif.items():
#rotate if necessary using orientation tags
if key == 274 and val == 3:
image = image.rotate(180, expand=True)
elif key == 274 and val == 6:
image = image.rotate(270, expand=True)
elif key == 274 and val == 8:
image = image.rotate(90, expand=True)
#EXTRACT METADATA FROM TAGS
#datetime
if key == 306:
metadata_datetime = val
if 306 not in img_exif.keys():
metadata_datetime = ''
#metadata_resolutionunit
if key == 296:
metadata_resolutionunit = val
if 296 not in img_exif.keys():
metadata_resolutionunit = ''
#metadata_Xresolution
if key == 282:
metadata_Xresolution = val
if 282 not in img_exif.keys():
metadata_Xresolution = ''
#metadata_Yresolution
if key == 283:
metadata_Yresolution = val
if 283 not in img_exif.keys():
metadata_Yresolution = ''
#metadata_brightnessvalue
if key == 37379:
metadata_brightnessvalue = val
if 37379 not in img_exif.keys():
metadata_brightnessvalue = ''
#metadata_aperturevalue
if key == 37378:
metadata_aperturevalue = val
if 37378 not in img_exif.keys():
metadata_aperturevalue = ''
#metadata_phonemake
if key == 271:
metadata_phonemake = val
if 271 not in img_exif.keys():
metadata_phonemake = ''
#metadata_phonemodel
if key == 272:
metadata_phonemodel = val
if 272 not in img_exif.keys():
metadata_phonemodel = ''
#metadata_ISOspeedratings
if key == 34855:
metadata_ISOspeedratings = val
if 34855 not in img_exif.keys():
metadata_ISOspeedratings = ''
#metadata_flash
if key == 37385:
metadata_flash = val
if 37385 not in img_exif.keys():
metadata_flash = ''
#metadata_focallength
if key == 37386:
metadata_focallength = val
if 37386 not in img_exif.keys():
metadata_focallength = ''
#metadata_sensingmethod
if key == 41495:
metadata_sensingmethod = val
if 41495 not in img_exif.keys():
metadata_sensingmethod = ''
#if ExifTags.TAGS[key] == 'Orientation':
# print(key) #274 is the key to the 'Orientation" TAG
#print(f'{ExifTags.TAGS[key]}:{val}')
# print(img_exif)
# print(type(img_exif))
# print('image after rotating')
# plt.imshow(image)
# plt.show()
#SAVE IMAGES METADATA
metadata_datetime_list.append(metadata_datetime)
metadata_resolutionunit_list.append(metadata_resolutionunit)
metadata_Xresolution_list.append(metadata_Xresolution)
metadata_Yresolution_list.append(metadata_Yresolution)
metadata_brightnessvalue_list.append(metadata_brightnessvalue)
metadata_aperturevalue_list.append(metadata_aperturevalue)
metadata_phonemake_list.append(metadata_phonemake)
metadata_phonemodel_list.append(metadata_phonemodel)
metadata_ISOspeedratings_list.append(metadata_ISOspeedratings)
metadata_flash_list.append(metadata_flash)
metadata_focallength_list.append(metadata_focallength)
metadata_sensingmethod_list.append(metadata_sensingmethod)
#SAVE IMAGE
image.save(Path.joinpath(imageFinalFilePath, f'{image_name}.{str(image_format)}'), format=image_format)
#remember to increment image_id
imageid += 1
#optionally - show image
if imageid % 250 == 0:
plt.imshow(image)
plt.show()
#CLOSE IMAGE (necessary to release memory, because load() function were not called)
image.close()
# if imageid > 6:
# break
#after all iterations
data_dictionary = {'name': images_names_list,
'mode': images_modes_list,
'format': images_formats_list,
'size': images_sizes_list,
'R': images_red_pixels_list,
'G': images_green_pixels_list,
'B': images_blue_pixels_list,
'source': images_sources_list,
'datetime': metadata_datetime_list,
'resolutionUnit': metadata_resolutionunit_list,
'Xresolution': metadata_Xresolution_list,
'Yresolution': metadata_Yresolution_list,
'brightness': metadata_brightnessvalue_list,
'aperture': metadata_aperturevalue_list,
'phonemake': metadata_phonemake_list,
'phonemodel': metadata_phonemodel_list,
'ISOspeedratings': metadata_ISOspeedratings_list,
'flash': metadata_flash_list,
'focallength': metadata_focallength_list,
'sensingmethod': metadata_sensingmethod_list}
df = pd.DataFrame(data_dictionary)
display(df)
df.to_csv(root_directory+'imagesmetadata2.csv')
# image from finaldata
image = Image.open('/content/drive/My Drive/2022_Projekt_badawczy/ZPD_SZUM/BAZA_Z_NETA/motorcycle/ZYM0ZRZ2MA9K.jpg')
img_exif = image.getexif()
print(img_exif)
if bool(img_exif) == False:
metadata_datetime = ''
print('Sorry, image has no exif data.')
id = None
print('TAG:value:key')
for key, val in img_exif.items():
print(f'{ExifTags.TAGS[key]}:{val}:{key}')
print("working", id)
image = Image.open('/content/drive/My Drive/2022_Projekt_badawczy/ZPD_SZUM/data/motorcycle/IMG_20220328_151748.jpg')
img_exif = image.getexif()
print('TAG:value:key')
for key, val in img_exif.items():
print(f'{ExifTags.TAGS[key]}:{val}:{key}')
# if key == 274 and val == 3:
# image = image.rotate(180, expand=True)
# elif key == 274 and val == 6:
# image = image.rotate(270, expand=True)
# elif key == 274 and val == 8:
# image = image.rotate(90, expand=True)
#TODO: EXTRACT METADATA FROM TAGS
#if ExifTags.TAGS[key] == 'Orientation':
# print(key) #274 is the key to the 'Orientation" TAG
The tags that were of our interest:
df = pd.read_csv(root_directory + 'imagesmetadata.csv', index_col=0)
df
Make column size as a list of tuples (instead of string) and add new columns with image height and image weidth (divide the tuple)
df['size'] = df['size'].apply(ast.literal_eval)
#type(df['size'])
df[['height', 'width']] = pd.DataFrame(df['size'].tolist(), index=df.index)
df
df.info()
df.nunique()
df.columns
df['mode'].value_counts()
df.format.value_counts()
df.format.unique()
df['size'].value_counts()
df['size'][df['size'] < (256, 256)].count()
df['size'].unique()
df.height.value_counts()
df.height[df.height < 256].count()
df.height.value_counts()[df.height.value_counts() > 10]
df.height.min()
df.width.min()
df.width.value_counts()
type(df.width.value_counts())
df.width.value_counts().sort_index()
df.width.value_counts()[df.width.value_counts() > 1]
df.width.value_counts()[df.width.value_counts() > 10]
df.width.value_counts().sort_index().plot.hist(bins=1200)
df.height.median(), df.width.median()
df.height.mean(), df.width.mean()
df.height.std(), df.width.std()
df.describe()
type(df.R)
df.R
df.G
initial_R_list = df.R
print(initial_R_list[0])
initial_G_list = df.G
print(initial_G_list[0])
print(list_of_R_strings)
print(list_of_G_strings)
df[6680:6685]
R_pixels_list = list()
G_pixels_list = list()
B_pixels_list = list()
final_R_list = list()
final_G_list = list()
final_B_list = list()
for index, row in df.iterrows():
try:
initial_R_list = row['R']
initial_G_list = row['G']
initial_B_list = row['B']
list_of_R_strings = initial_R_list.strip('][').split(', ')
list_of_G_strings = initial_G_list.strip('][').split(', ')
list_of_B_strings = initial_B_list.strip('][').split(', ')
list_of_R_ints = list(map(int, list_of_R_strings))
list_of_G_ints = list(map(int, list_of_G_strings))
list_of_B_ints = list(map(int, list_of_B_strings))
final_R_list.append(list_of_R_ints)
final_G_list.append(list_of_G_ints)
final_B_list.append(list_of_B_ints)
except:
#TODO: inwestigate this 3 indexes: 6682, 6807, 6851
print("wrong index: ", index)
print(initial_R_list)
print(initial_G_list)
print(initial_B_list)
# print(list_of_R_strings)
# print(list_of_G_strings)
# print(list_of_R_ints)
# print(list_of_G_ints)
R_pixels_list = [sum(x) for x in zip(*final_R_list)]
G_pixels_list = [sum(x) for x in zip(*final_G_list)]
B_pixels_list = [sum(x) for x in zip(*final_B_list)]
print(type(G_pixels_list))
print(G_pixels_list)
number_of_pictures = len(df)
number_of_pictures
finalRedList = [x / number_of_pictures for x in R_pixels_list]
finalGreenList = [x / number_of_pictures for x in G_pixels_list]
finalBlueList = [x / number_of_pictures for x in B_pixels_list]
print(finalRedList)
print(finalGreenList)
print(finalBlueList)
def getRed(R): return '#%02x%02x%02x'%(R,0,0)
def getGreen(G): return '#%02x%02x%02x'%(0,G,0)
def getBlue(B):return '#%02x%02x%02x'%(0,0,B)
plt.figure(0) # plots a figure to display RED Histogram
for i in range(0, 256):
plt.title("Histogram of RED pixels")
plt.xlabel('Wartość pixela')
plt.ylabel('Ilość pixeli')
plt.bar(i, finalRedList[i], color = getRed(i)) #, alpha=0.3
red = finalRedList # indicates Red
green = finalGreenList # indicated Green
blue = finalBlueList # indicates Blue
plt.figure(0) # plots a figure to display RED Histogram
for i in range(0, 256):
plt.title("Histogram of RED pixels")
plt.xlabel('Value of pixel') # Wartość pixela
plt.ylabel('Number of pixels') # Ilość pixeli
plt.bar(i, red[i], color = getRed(i)) #, alpha=0.3
plt.savefig(plots + '/red_bar_hist.jpg', dpi=600)
plt.figure(1) # plots a figure to display GREEN Histogram
for i in range(0, 256):
plt.title("Histogram of GREEN pixels")
plt.xlabel('Value of pixel')
plt.ylabel('Number of pixels')
plt.bar(i, green[i], color = getGreen(i))
plt.savefig(plots + '/green_bar_hist.jpg', dpi=600)
plt.figure(2) # plots a figure to display BLUE Histogram
for i in range(0, 256):
plt.title("Histogram of BLUE pixels")
plt.xlabel('Value of pixel')
plt.ylabel('Number of pixels')
plt.bar(i, blue[i], color = getBlue(i))
plt.savefig(plots + '/blue_bar_hist.jpg', dpi=600)
plt.figure(3) # plots a figure to display RGB Histogram
for i in range(0, 256):
plt.title("Bar histogram of RGB pixels")
plt.xlabel('Value of pixel')
plt.ylabel('Number of pixels')
plt.bar(i, blue[i], color = getBlue(i))
plt.bar(i, red[i], color = getRed(i))
plt.bar(i, green[i], color = getGreen(i))
plt.savefig(plots + '/RGB_bar_hist.jpg', dpi=600)
plt.figure(4) # plots a figure to display RGB Histogram
plt.title("Line histogram of RGB pixels")
plt.xlabel('Value of pixel')
plt.ylabel('Number of pixels')
plt.plot(red, color='red')
plt.plot(green, color='green')
plt.plot(blue, color='blue')
plt.savefig(plots + '/RGB_line_hist.jpg', dpi=600)
plt.show()
df.iloc[[6682, 6807, 6851],:]
cwd = os.getcwd() # Get the current working directory (cwd)
files = os.listdir(cwd + '/drive/MyDrive/2022_Projekt_badawczy/ZPD_SZUM/finaldata/car') # Get all the files in that directory
print("Files in %r: %s" % (cwd, files))
Case of image resizing:
#trying to load this picture
#Image cannot be found:
#FileNotFoundError: [Errno 2] No such file or directory:
#'/content/drive/My Drive/2022_Projekt_badawczy/ZPD_SZUM/finaldata/car/car3532.jpg'
# ANSWER: I need to provided .JPEG with capital letters at the end of the file name
image2 = Image.open(final_data + 'car/car3532.JPEG')
plt.imshow(image2);
print(image2.size)
img_resized = image2.resize((256,256))
print(img_resized.size)
plt.imshow(img_resized);
Case of image stretching:
df[df['size'] < (256,256)]
#bus1100, bus1101 - 64x64
#truck157 - 250x166
#truck160 - 120x120
#in case of the bus, the streching is hard to see by eye
image2 = Image.open(final_data + 'bus/bus1100.JPEG')
plt.imshow(image2)
plt.show()
print(image2.size)
img_resized = image2.resize((256,256))
print(img_resized.size)
plt.imshow(img_resized)
plt.show()
#for this car, streching works fine
image2 = Image.open(final_data + 'truck/truck157.JPEG')
plt.imshow(image2)
plt.show()
print(image2.size)
img_resized = image2.resize((256,256))
print(img_resized.size)
plt.imshow(img_resized)
plt.show()
# and for this car, streching works fine too
image2 = Image.open(final_data + 'truck/truck160.JPEG')
plt.imshow(image2)
plt.show()
print(image2.size)
img_resized = image2.resize((256,256))
print(img_resized.size)
plt.imshow(img_resized)
plt.show()
Preprocessed data will be saved in "preprocesessed" folder.
df
tmp_df = df.copy()
tmp_df
type(tmp_df.source[tmp_df.name == 'bus30'].item())
class_names = ['motorcycle', 'bicycle', 'bus', 'car', 'truck']
images_names_list = list()
images_modes_list = list()
images_formats_list = list()
images_sizes_list = list()
images_heights_list = list()
images_widths_list = list()
images_red_pixels_list = list()
images_green_pixels_list = list()
images_blue_pixels_list = list()
images_sources_list = list()
metadata_datetime_list = list()
metadata_resolutionunit_list = list()
metadata_Xresolution_list = list()
metadata_Yresolution_list = list()
metadata_brightnessvalue_list = list()
metadata_aperturevalue_list = list()
metadata_phonemake_list = list()
metadata_phonemodel_list = list()
metadata_ISOspeedratings_list = list()
metadata_flash_list = list()
metadata_focallength_list = list()
metadata_sensingmethod_list = list()
for class_name in class_names:
#paths
imageFinalFilePath = Path.joinpath(final_data_dir, class_name)
imagePreprocessedFilePath = Path(preprocessed_data, class_name)
#collect images from first source
for imageid, imagePath in enumerate(list(imageFinalFilePath.glob('*.*'))):
#each class starts id from 0
# load image
image = Image.open(imagePath)
# PERFORM RESIZING
img_resized = image.resize((256,256))
image = img_resized
# save in the JPEG format
#/content/drive/MyDrive/2022_Projekt_badawczy/ZPD_SZUM/finaldata/motorcycle/motorcycle1537.JPEG
str_to_split = "/content/drive/My Drive/2022_Projekt_badawczy/ZPD_SZUM/finaldata/" + class_name + "/"
image_name = str(imagePath).split(str_to_split)[1]
image_name = image_name.split('.')[0]
image_mode = image.mode
if image_mode == 'RGBA':
image = image.convert(mode='RGB')
image_mode = image.mode
image_format = 'JPEG'
image_size = image.size
image_height = image.size[0]
image_width = image.size[1]
#image source
image_source = tmp_df.source[tmp_df.name == image_name].item()
pixels = image.histogram()
red_pixels = pixels[0:256] # indicates Red
green_pixels = pixels[256:512] # indicated Green
blue_pixels = pixels[512:768] # indicates Blue
#print(image_name, image_mode, image_format, image_size, red_pixels, green_pixels, blue_pixels)
#SAVE IMAGES DATA
images_names_list.append(image_name)
images_modes_list.append(image_mode)
images_formats_list.append(image_format)
images_sizes_list.append(image_size)
images_heights_list.append(image_height)
images_widths_list.append(image_width)
images_red_pixels_list.append(red_pixels)
images_green_pixels_list.append(green_pixels)
images_blue_pixels_list.append(blue_pixels)
images_sources_list.append(image_source)
#EXTRACT METADATA FROM TAGS
img_exif = image.getexif()
if bool(img_exif) == False:
metadata_datetime = ''
metadata_resolutionunit = ''
metadata_Xresolution = ''
metadata_Yresolution = ''
metadata_brightnessvalue = ''
metadata_aperturevalue = ''
metadata_phonemake = ''
metadata_phonemodel = ''
metadata_ISOspeedratings = ''
metadata_flash = ''
metadata_focallength = ''
metadata_sensingmethod = ''
#print('Sorry, image has no exif data.')
else:
for key, val in img_exif.items():
#datetime
if key == 306:
metadata_datetime = val
if 306 not in img_exif.keys():
metadata_datetime = ''
#metadata_resolutionunit
if key == 296:
metadata_resolutionunit = val
if 296 not in img_exif.keys():
metadata_resolutionunit = ''
#metadata_Xresolution
if key == 282:
metadata_Xresolution = val
if 282 not in img_exif.keys():
metadata_Xresolution = ''
#metadata_Yresolution
if key == 283:
metadata_Yresolution = val
if 283 not in img_exif.keys():
metadata_Yresolution = ''
#metadata_brightnessvalue
if key == 37379:
metadata_brightnessvalue = val
if 37379 not in img_exif.keys():
metadata_brightnessvalue = ''
#metadata_aperturevalue
if key == 37378:
metadata_aperturevalue = val
if 37378 not in img_exif.keys():
metadata_aperturevalue = ''
#metadata_phonemake
if key == 271:
metadata_phonemake = val
if 271 not in img_exif.keys():
metadata_phonemake = ''
#metadata_phonemodel
if key == 272:
metadata_phonemodel = val
if 272 not in img_exif.keys():
metadata_phonemodel = ''
#metadata_ISOspeedratings
if key == 34855:
metadata_ISOspeedratings = val
if 34855 not in img_exif.keys():
metadata_ISOspeedratings = ''
#metadata_flash
if key == 37385:
metadata_flash = val
if 37385 not in img_exif.keys():
metadata_flash = ''
#metadata_focallength
if key == 37386:
metadata_focallength = val
if 37386 not in img_exif.keys():
metadata_focallength = ''
#metadata_sensingmethod
if key == 41495:
metadata_sensingmethod = val
if 41495 not in img_exif.keys():
metadata_sensingmethod = ''
#SAVE IMAGES METADATA
metadata_datetime_list.append(metadata_datetime)
metadata_resolutionunit_list.append(metadata_resolutionunit)
metadata_Xresolution_list.append(metadata_Xresolution)
metadata_Yresolution_list.append(metadata_Yresolution)
metadata_brightnessvalue_list.append(metadata_brightnessvalue)
metadata_aperturevalue_list.append(metadata_aperturevalue)
metadata_phonemake_list.append(metadata_phonemake)
metadata_phonemodel_list.append(metadata_phonemodel)
metadata_ISOspeedratings_list.append(metadata_ISOspeedratings)
metadata_flash_list.append(metadata_flash)
metadata_focallength_list.append(metadata_focallength)
metadata_sensingmethod_list.append(metadata_sensingmethod)
#SAVE IMAGE
#image.save(Path.joinpath(imagePreprocessedFilePath, f'{image_name}.{str(image_format)}'), format=image_format)
#optionally - show image
if imageid % 250 == 0:
plt.imshow(image)
plt.show()
#CLOSE IMAGE (necessary to release memory, because load() function were not called)
image.close()
#after all iterations
data_dictionary = {'name': images_names_list,
'mode': images_modes_list,
'format': images_formats_list,
'img_size': images_sizes_list,
'height': images_heights_list,
'width': images_widths_list,
'R': images_red_pixels_list,
'G': images_green_pixels_list,
'B': images_blue_pixels_list,
'source': images_sources_list,
'datetime': metadata_datetime_list,
'resolutionUnit': metadata_resolutionunit_list,
'Xresolution': metadata_Xresolution_list,
'Yresolution': metadata_Yresolution_list,
'brightness': metadata_brightnessvalue_list,
'aperture': metadata_aperturevalue_list,
'phonemake': metadata_phonemake_list,
'phonemodel': metadata_phonemodel_list,
'ISOspeedratings': metadata_ISOspeedratings_list,
'flash': metadata_flash_list,
'focallength': metadata_focallength_list,
'sensingmethod': metadata_sensingmethod_list}
preprocessed_df = pd.DataFrame(data_dictionary)
display(preprocessed_df)
#save to csv file
preprocessed_df.to_csv(preprocessed_data + 'preprocesseddata.csv')
Image preprocessing works well, but there are some problems with preprocessed_df, so we will not use it.
hst = image.histogram()
red = hst[0:256] # indicates Red
green = hst[256:512] # indicated Green
blue = hst[512:768] # indicates Blue
plt.figure(0) # plots a figure to display RED Histogram
for i in range(0, 256):
plt.title("Histogram of RED pixels")
plt.xlabel('Wartość pixela')
plt.ylabel('Ilość pixeli')
plt.bar(i, red[i], color = getRed(i)) #, alpha=0.3
plt.figure(1) # plots a figure to display GREEN Histogram
for i in range(0, 256):
plt.title("Histogram of GREEN pixels")
plt.xlabel('Wartość pixela')
plt.ylabel('Ilość pixeli')
plt.bar(i, green[i], color = getGreen(i))
plt.figure(2) # plots a figure to display BLUE Histogram
for i in range(0, 256):
plt.title("Histogram of BLUE pixels")
plt.xlabel('Wartość pixela')
plt.ylabel('Ilość pixeli')
plt.bar(i, blue[i], color = getBlue(i))
plt.figure(3) # plots a figure to display RGB Histogram
for i in range(0, 256):
plt.title("Bar histogram of RGB pixels")
plt.xlabel('Wartość pixela')
plt.ylabel('Ilość pixeli')
plt.bar(i, blue[i], color = getBlue(i))
plt.bar(i, red[i], color = getRed(i))
plt.bar(i, green[i], color = getGreen(i))
plt.figure(4) # plots a figure to display RGB Histogram
plt.title("Line histogram of RGB pixels")
plt.xlabel('Wartość pixela')
plt.ylabel('Ilość pixeli')
plt.plot(red, color='red')
plt.plot(green, color='green')
plt.plot(blue, color='blue')
plt.show()
class_names = ['bicycle', 'bus', 'car', 'motorcycle', 'truck']
for class_name in class_names:
imageid = 0
#image = collected_data_dir+class_name#.glob('*/*.jpg')
imageCollectedFilePath = Path.joinpath(collected_data_dir, class_name)
imageDownloadedFilePath = Path.joinpath(downloaded_data_dir, class_name)
imageFinalFilePath = Path.joinpath(final_data_dir, class_name)
#print(imageFinalFilePath)
#print(imageFilePath)
for imagePath in list(imageCollectedFilePath.glob('*.*')):
#print(imageid)
#print(imagePath)
image = Image.open(imagePath) # ładuje zdjęcie (read_image)
# save in the same format
#print(image.format)
#print(Path.joinpath(imageFinalFilePath, f'{class_name + str(imageid)}.{str(image.format)}'))
image_name = class_name + str(imageid)
image_mode = image.mode
image_format = image.format
image_size = image.size
#print(image_name, image_mode, image_format, image_size)
#SAVE IMAGE
#image.save(Path.joinpath(imageFinalFilePath, f'{image_name}.{str(image_format)}'), format=image_format)
#remember to increment image_id
imageid += 1
hst = image.histogram()
red = hst[0:256] # indicates Red
green = hst[256:512] # indicated Green
blue = hst[512:768] # indicates Blue
plt.figure(0) # plots a figure to display RED Histogram
for i in range(0, 256):
plt.title("Histogram of RED pixels")
plt.xlabel('Wartość pixela')
plt.ylabel('Ilość pixeli')
plt.bar(i, red[i], color = getRed(i)) #, alpha=0.3
plt.figure(1) # plots a figure to display GREEN Histogram
for i in range(0, 256):
plt.title("Histogram of GREEN pixels")
plt.xlabel('Wartość pixela')
plt.ylabel('Ilość pixeli')
plt.bar(i, green[i], color = getGreen(i))
plt.figure(2) # plots a figure to display BLUE Histogram
for i in range(0, 256):
plt.title("Histogram of BLUE pixels")
plt.xlabel('Wartość pixela')
plt.ylabel('Ilość pixeli')
plt.bar(i, blue[i], color = getBlue(i))
plt.show()
# r, g, b = image.split()
# print("histogram length: ", len(image.histogram()))
# #display(r.histogram())
# plt.hist(image.histogram(), bins=None)
# #optionally - show image
# #plt.imshow(image)
# plt.show()
break
if imageid == 2:
break
break
256*3
r, g, b = img.split()
len(r.histogram())
### 256 ###
r.histogram()
class_names = ['bicycle', 'bus', 'car', 'motorcycle', 'truck']
for class_name in class_names:
#image = collected_data_dir+class_name#.glob('*/*.jpg')
imageFilePath = Path.joinpath(collected_data_dir, class_name)
#print(imageFilePath)
for imagePath in list(imageFilePath.glob('*.*')):
#print(imagePath)
image = Image.open(imagePath) # ładuje zdjęcie (read_image)
#todo: imageid
print("image: ", image)
print(image.format)
print(image.mode)
print(image.size)
data = np.asarray(image)
#print("data: ", data)
# summarize shape
#print("data shape: ", data.shape)
# create Pillow image
image2 = Image.fromarray(data)
print("image2: ", image2)
# summarize image details
print(image2.format)
print(image2.mode)
print(image2.size)
plt.imshow(image)
plt.show()
break
break
from os import listdir
# load all images in a directory
loaded_images = list()
for filename in listdir(collected_data + '/bicycle'):
# load image
img_data = image.imread(collected_data + '/bicycle/' + filename)
#print(img_data)
# store loaded image
loaded_images.append(img_data)
print('> loaded %s %s' % (filename, img_data.shape))
break
filename